package yns.springboot.guide.demo.aop;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAnnotationAspect {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Pointcut("@annotation(yns.springboot.guide.demo.aop.annotation.LogAnnotation)")
	public void logAnnotation() {
		logger.info("LogAnnotationAspect logAnnotation method");
	}

	@Around("logAnnotation()")
	public Object Interceptor(ProceedingJoinPoint proceedingJoinPoint) {
		logger.info("LogAnnotationAspect Interceptor method");

		Object[] args = proceedingJoinPoint.getArgs();
		Class<? extends Object> clazz = proceedingJoinPoint.getTarget().getClass();
		Signature signature = proceedingJoinPoint.getSignature();

		logger.info("LogAnnotationAspect Interceptor : Args=" + Arrays.asList(args) + ",Method=" + signature
				+ ",TargetClass=" + clazz);

		Object result = null;
		try {
			result = proceedingJoinPoint.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return result;
	}
}
